package com.wyzz.hr.bo.impl;

import com.wyzz.hr.bo.BackupRBo;
import com.wyzz.hr.dao.BackupDao;
import com.wyzz.hr.dao.impl.BackUpTools;
import com.wyzz.hr.pojo.Backup;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.SysConfigManager;
import com.wyzz.hr.util.impl.PropertiesFileConfigManager;

import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class BackupRBoImpl implements BackupRBo {

	public BackupRBoImpl() {
		backupDAO = null;
	}

	public boolean execSqlCmd(String mysqlBinPath, String filePath,
			String sqlCmdStr) {
		return BackUpTools.execSqlCmd(mysqlBinPath, filePath, sqlCmdStr);
	}

	public List searchAll(String filePath, Pager pager) {
		File file = new File(filePath);
		if (!file.exists())
			return new ArrayList();
		File files[] = file.listFiles();
		List backList = new ArrayList();
		for (int i = 0; i < files.length; i++) {
			File newFile = files[i];
			if (!newFile.isFile())
				continue;
			String fileName = newFile.getName();
			if (fileName.length() < 20)
				continue;
			Backup backup = new Backup();
			backup.setFileName(fileName);
			backup.setFileSize(Integer.valueOf((int) newFile.length() / 1024));
			String time = fileName.substring(7, fileName.lastIndexOf("."));
			if (time.length() != 19) {
				backup.setFileCreatTime(time);
			} else {
				time = (new StringBuilder()).append(time.substring(0, 10))
						.append(" ").append(
								time.substring(11).replaceAll("-", ":"))
						.toString();
				backup.setFileCreatTime(time);
			}
			backList.add(backup);
		}

		List list = new ArrayList();
		int count = 0;
		for (; backList.size() > 0; backList.remove(count)) {
			Backup back = (Backup) backList.get(0);
			count = 0;
			for (int i = 1; i < backList.size(); i++)
				if (back.getFileCreatTime().compareTo(
						((Backup) backList.get(i)).getFileCreatTime()) < 0) {
					back = (Backup) backList.get(i);
					count = i;
				}

			list.add(back);
		}

		splitPage(list, pager);
		if (list.size() < pager.getPageSize())
			return list;
		int begain = pager.getPageSize() * (pager.getCurrentPage() - 1);
		int end = pager.getPageSize() + begain;
		if (end > list.size())
			end = list.size();
		List newList = new ArrayList();
		for (int i = begain; i < end; i++)
			newList.add(list.get(i));

		return newList;
	}

	public BackupDao getBackupDAO() {
		return backupDAO;
	}

	public void setBackupDAO(BackupDao backupDAO) {
		this.backupDAO = backupDAO;
	}

	private void splitPage(List list, Pager page) {
		int size = list.size();
		SysConfigManager sysConfigManager = PropertiesFileConfigManager
				.getInstance();
		int pageSize = Integer.valueOf(
				DatabaseSysConfigManager.getInstance().getProperty(
						"sys.split.pages")).intValue();
		page.init(size, pageSize);
		if (page.getOperate() != null
				&& "previous".equalsIgnoreCase(page.getOperate()))
			page.previous();
		if (page.getOperate() != null
				&& "next".equalsIgnoreCase(page.getOperate()))
			page.next();
		if (page.getOperate() != null
				&& "first".equalsIgnoreCase(page.getOperate()))
			page.first();
		if (page.getOperate() != null
				&& "last".equalsIgnoreCase(page.getOperate()))
			page.last();
	}

	public String sqlbin(String path) {
		if (!(new File(path)).exists()) {
			System.out.println((new StringBuilder()).append(
					"path doesn't exist").append(path).toString());
			return "error";
		} else {
			SysConfigManager sysConfigManager = PropertiesFileConfigManager
					.getInstance();
			sysConfigManager.setProperty("sys.security.sqlbin.path", path);
			sysConfigManager.saveChange();
			return "success";
		}
	}

	private BackupDao backupDAO;
}
